<?php


namespace App\Service;


use App\Jobs\TaskJob;
use App\Models\SiteGold;
use App\Models\UserGold;

class Queue
{

    // 消息队列操作
    public function __construct()
    {

    }

    // 会员做任务 获取积分金币
    public function memberTask($uid, $field)
    {
        $goldDb = new SiteGold();
        // 获取任务信息
        $taskInfo = $goldDb->isCache(true)->oneFind(['field' => $field]);
        if(empty($taskInfo)){
            throw new \RuntimeException('任务不存在', 400);
        }


        $uGoldDb = new UserGold();
        $result  = $uGoldDb->isFinish($taskInfo, $uid);
        if($result){
            throw new \RuntimeException('任务已经完成！', 400);
        }

        // 发送任务
        //TaskJob::dispatchIf(true, $data)->onQueue('member_task');
        $data = ['uid' => $uid, 'field' => $field];
        $res  = TaskJob::dispatch($data)->onQueue('member_task');

        return $res;
    }

    // 各种审核操作
    public function toVerify($model, $id)
    {


        // 发送任务
        $data = ['id' => $id, 'model' => $model];
        $res  = TaskJob::dispatch($data)->onQueue('to_verify');

        return $res;
    }

}


